<template>
  <basic-container>
    <avue-crud v-if="tableOption" v-bind="bindVal" v-on="onEvent" :search.sync="searchParams" v-model:page="page"
               v-model="startForm">
      <template #menu-left>
        <el-button type="primary" icon="el-icon-plus" @click="$refs.importTable.show()">导入数据表
        </el-button>
        <el-button type="primary" :disabled="selectList.length<1" icon="el-icon-plus" @click="batchGenCode">
          批量生成代码
        </el-button>
      </template>
      <template #menu-btn="{ type, size, row }">
        <el-dropdown-item type="primary" size="small" icon="el-icon-plus" @click.native="createCode(row)">生成代码
        </el-dropdown-item>
      </template>
    </avue-crud>
    <gen-code ref="genCode"></gen-code>
    <import-table ref="importTable" @selectConfirm="confirmImportTable"></import-table>

  </basic-container>
</template>

<script>
import {importTables} from "@/api/start/development/sysDataTableConfigApi";
import ImportTable from "@/views/start/development/table/importTable.vue";
import {createDynamicMixin} from "../../../../mixins/startCrud.js";

import autoRouter from "./autoRouter";
import {createDownloadFile} from "utils/util";
import {genCode} from "@/api/start/development/dataTableApi";

const configOption = {}
configOption.name = "start/development/dataTable"


export default {
  name: "index",
  mixins: [createDynamicMixin(configOption)],
  components: {

    ImportTable,

  },
  watch: {
    'searchParams.dataSourcesKey': function (val) {
      this.refreshChange();
    }
  },
  methods: {
    createCode(row) {
      genCode(row.id).then(res => {
        createDownloadFile(res, "startCode.zip")
      })
    },
    batchGenCode() {
      let ids = this.selectList.map(e => e.id)
      if (ids < 1) {
        this.$message.warning("至少选择一条数据")
        return
      }
      genCode(ids).then(res => {
        createDownloadFile(res, "startCode.zip")
      })
    },


    confirmImportTable(tables, done) {
      console.log("tables", tables)
      if (!tables || tables.length < 1) {
        this.$message.error("至少选择一条数据")
        return
      }
      // console.log("tables",tables)
      this.$confirm("您将导入数据表：" + tables.map(e => e.tableName) + "是否确认导入").then(res => {
        return importTables(tables)
      }).then(res => {
        this.$message.success("导入成功")
        this.loadRemoteDataSources()
        if (done) {
          done();
        }
      })
    },
    beforeOpenAddDialog() {
      console.log("add", "")
    },
    beforeOpenEditDialog(row) {
      let params = {}
      params.tableId = row.id
      params.tableName = "编辑数据表【" + row.tableComment + "】"
      this.$router.push({name: autoRouter.names.editTable, params})
    },
    beforeOpenViewDialog() {
      console.log("view")
    },
    editTable(row) {

    }

  }
}
</script>

<style scoped></style>
